<template>
    <el-form ref="form" :model="HomemasterForm" label-width="auto" size="medium" label-position="left" :rules="rules">  
        <h3>添加宿管</h3>
        <el-form-item label="工号：" prop="hmid">
            {{hmid}}
        </el-form-item>
        <el-form-item label="姓名：" prop="name">
            <el-input v-model="HomemasterForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别：" >
            <el-radio v-model="HomemasterForm.sex" label="男">男</el-radio>
            <el-radio v-model="HomemasterForm.sex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="电话：" prop="phone">
            <el-input maxlength="11" v-model="HomemasterForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱：" prop="email">
            <el-input v-model="HomemasterForm.email"></el-input>
        </el-form-item>
        <el-form-item size="large" label-width="0" style="text-align: center;"> 
            <el-button type="primary" @click="onSubmit(HomemasterForm)">立即创建</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    name:'Addhomematser',
    data() {
        return {
            HomemasterForm:{
                hmid:'',
                name:'',
                sex:'男',
                phone:'',
                email:''
            },
            rules:{
                name:[{ required:true,message:'姓名不能为空',trigger:'blur' }],
                phone:[{ required:true,message:'电话不能为空',trigger:'blur' }],
                email:[{ required:true,message:'邮箱不能为空',trigger:'blur' }],
            }
        }
    },
    props:['hmid'],
    methods:{
        onSubmit(HomemasterForm){
            let _this = this
            let arr = Array('qq.com','163.com','139.com','sina.com','yeah.com','126.com','21cn.com','gmail.com','yahoo.com','msn.com','hotmail.com','aol.com','ask.com','live.com','3721.net','googlemail.com','mail.com','0355.net')
            if(HomemasterForm.phone.length < 11 || isNaN(parseInt(HomemasterForm.phone)) || HomemasterForm.phone.slice(0,1) != '1'){
                alert('电话输入有误')
                return
            }
            if(HomemasterForm.email.split('@')[0] == '' || !arr.includes(HomemasterForm.email.split('@')[1])){
                alert('邮箱地址有误')
                return
            }
            _this.HomemasterForm.hmid = _this.hmid
            axios.get('http://localhost:8081/homemaster/addHomemaster',{params:HomemasterForm}).then(function(resp){
                alert('添加成功')
                _this.HomemasterForm.name = ''
                _this.HomemasterForm.sex = '男'
                _this.HomemasterForm.phone = ''
                _this.HomemasterForm.email = ''
                _this.getHmid()
            }).catch(function(err){
                alert('添加失败')
            })
        },
        getHmid(){
            const _this = this
            axios.get(`http://localhost:8081/homemaster/countAllByYear?year=${new Date().getFullYear()}`).then((res)=>{
                let time = new Date().getFullYear();
                let a
                if(res.data<10)
                    a = "00"+ (res.data+1)
                else if(res.data<100)
                    a = "0" + (res.data+1)
                _this.hmid = "" + time + a
            })
        },
    },
}
</script>

<style scoped>
    .el-form{
        width: 500px;
    }
    .el-form-item{
        text-align: left;
        display: block;
    }
</style>